package com.shy.config.handler;

import com.shy.config.common.JWTConstants;
import com.shy.config.common.TokenConstants;
import com.shy.config.model.R;
import com.shy.config.util.ResponseUtils;
import com.shy.service.ClueService;
import com.shy.service.DicValueService;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 登出成功处理器
 */
@Component
public class LogoutSuccessHandlers implements LogoutSuccessHandler {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private DicValueService dicValueService;
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        /*从请求头中获取token*/
        String header = request.getHeader(TokenConstants.AUTHORIZATION);
        /*获取token值*/
        String token = header.replaceFirst(TokenConstants.BEARER, "");
        /*清除redis中的token*/
        stringRedisTemplate.delete(JWTConstants.JWT_KEY + token);
        //清除dic表中的缓存
//        dicValueService.evictAllCaches();
        /*响应数据给前端*/
        ResponseUtils.write(response, R.success("登出成功"));
        System.out.println("用户已登出.....");
    }
}
